import urls from '/util/urls' 
import { alert } from '/util/utils'

Page({
  data: {},
  onSubmit(e) {
    let formData = {
      ...e.detail.value
    }
    if(!formData.username || !formData.password) {
      alert('请输入用户名和密码')
      return
    }
    dd.getAuthCode({
        success:(res)=>{
            this.setData({
                authCode:res.authCode
            })
            //dd.alert({content: "step1"});
            dd.httpRequest({
                url: urls.bindUrl(),
                method: 'POST',
                headers:{"Content-Type":"application/json"},
                data: JSON.stringify({
                  authCode: res.authCode,
                   ...e.detail.value
                }),
                dataType: 'json',
                success: (res) => {
                    if(res.data.status === 'failure') {
                      alert(res.data.data)
                    } else {
                      dd.redirectTo({
                        url: '/pages/index/index'
                      })
                    }
                },
                fail: (res) => {
                    alert(res.data.data)
                },
                complete: (res) => {
                    dd.hideLoading();
                }
                
            });
        },
        fail: (err)=>{
            // dd.alert({
            //     content: JSON.stringify(err)
            // })
            // alert(res.data.data)
        }
    })
  }
});
